package com.ss.android.article.base.feature.model;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.newmedia.activity.browser.BrowserActivity;
import com.ss.android.newmedia.util.AppUtil;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AppAd extends BaseAdsData {
    private boolean a;
    public int h;
    public String appAdSource;
    public String appAdSourceLabel;
    public String appAdDescription;
    public String appAdTitle;
    public ImageInfo imageInfo;
    public List imageList;
    public boolean o;
    public int p;

    public AppAd(int arg2) {
        this.o = false;
        this.a = false;
        this.p = 0;
        this.h = arg2;
    }

    public boolean isValid() {
        return this.adType == 1 && super.isValid();
    }

    public static boolean a(int arg2) {
        boolean v0 = true;
        if(arg2 != 2 && arg2 != 1 && arg2 != 4 && arg2 != 3) {
            v0 = false;
        }

        return v0;
    }

    public final void a(Context arg6, int appAdFrom) {
        if(TTUtils.isValidUrl(this.webUrl)) {
            Intent v0 = new Intent(arg6, BrowserActivity.class);
            v0.setData(Uri.parse(this.webUrl));
            if(!android.support.design.a.isTextEmpty(this.webTitle)) {
                v0.putExtra("title", this.webTitle);
            }

            if(AppData.getInstance().allowInsideDownloadManager()) {
                v0.putExtra("bundle_is_from_app_ad", true);
                v0.putExtra("bundle_app_ad_from", appAdFrom);
                v0.putExtra("bundle_download_url", this.downloadUrl);
                v0.putExtra("bundle_download_app_name", this.appName);
                v0.putExtra("bundle_app_package_name", this.pkg);
                v0.putExtra("bundle_download_app_extra", String.valueOf(this.id));
                v0.putExtra("bundle_download_app_log_extra", this.logExtra);
                v0.putExtra("ad_id", this.id);
            }

            v0.putExtra("use_swipe", true);
            arg6.startActivity(v0);
        }
    }

    public final void a(Context arg27, int arg28, DownloadShortInfo arg29, DownloadInfoChangeListener arg30, int arg31) {
        JSONObject v12;
        if(arg27 != null) {
            String v6 = null;
            String v14 = null;
            String v7 = "open";
            switch(arg31) {
                case 1: {
                    v6 = "embeded_ad";
                    v14 = "embeded_ad";
                    break;
                }
                case 2: {
                    v6 = "detail_ad";
                    v14 = "detail_ad";
                    break;
                }
                case 3: {
                    v6 = "comment_ad";
                    v14 = "comment_ad";
                    break;
                }
                case 4: {
                    v6 = "wap";
                    v14 = "wap";
                    break;
                }
                case 5: {
                    v6 = "detail_download_ad";
                    v14 = "detail_ad";
                    v7 = "click_open";
                    break;
                }
                case 6: {
                    v6 = "detail_download_ad";
                    v14 = "detail_download_ad";
                    v7 = "click_open";
                    break;
                }
                case 7: {
                    v6 = "feed_download_ad";
                    v14 = "feed_download_ad";
                    v7 = "click_open";
                    break;
                }
            }

            this.b(arg31);
            JSONObject v4 = null;
            try {
                if(!android.support.design.a.isTextEmpty(this.logExtra)) {
                    v4 = new JSONObject();
                    v4.put("log_extra", this.logExtra);
                }
                v12 = v4;
            } catch(Exception v4_1) {
                v12 = null;
            }
            if(!android.support.design.a.isTextEmpty(this.openUrl) && (AppUtil.c(arg27, this.openUrl, this.pkg))) {
                MobAdClickCombiner.onAdEvent(arg27, v6, v7, this.id, 0, v12, this.p);
                return;
            }

            if(!AppUtil.a(arg27, this.version, this.pkg) && (AppUtil.c(arg27, this.openUrl, this.pkg))) {
                MobAdClickCombiner.onAdEvent(arg27, v6, v7, this.id, 0, v12, this.p);
                return;
            }

            int v4_2 = arg29 == null || arg29.b != 8 ? 0 : 1;
            if(arg29 != null) {
                try {
                    if(android.support.design.a.isTextEmpty(this.alertText)) {
                    }else {
                        this.o = true;
                    }
                }
                catch(Exception v5) {
                }
            }

            switch(arg28) {
                case 1: {
                    if(this.o) {
                        if(arg29 != null&&(arg29.b == 2 || arg29.b == 1)) {
                               MobAdClickCombiner.onAdEvent(arg27, v6, "click_pause_ad", this.id, 1, v12, this.p);
                        } else {
                            if(arg29 == null) {
                            }else

                            if(arg29.b != 4) {
                            }else {
                                MobAdClickCombiner.onAdEvent(arg27, v6, "click_continue_ad", this.id, 1, v12, this.p);
                            }
                        }

                        if(v4_2 == 0) {
                            return;
                        }

                        this.a(arg27, arg29, arg30, this.id, 1, v12, arg31);
                        return;
                    }

                    AppUtil.b(this.clickTrackUrlList, arg27);
                    MobAdClickCombiner.onAdEvent(arg27, v14, "click", this.id, 1, v12, this.p);
                    if((android.support.design.a.isTextEmpty(this.alertText)) && v4_2 == 0) {
                        v7 = "feeds_detail";
                        this.a(arg27, arg31);
                        if(arg31 == 1) {
                            MobAdClickCombiner.onAdEvent(arg27, v6, v7, this.id, 0, v12, 2);
                            v4_2 = 0;
                        } else {
                            v4_2 = 0;
                        }
                        break;
                    }

                    if(v4_2 != 0) {
                        this.a(arg27, arg29, arg30, this.id, 1, v12, arg31);
                        v4_2 = 0;
                    }else {
                        v4_2 = 1;
                    }
                    break;
                }
                case 2: {
                    this.a(arg27, arg29, arg30, this.id, 2, v12, arg31);
                    v4_2 = 0;
                    break;
                }
                default: {
                    v4_2 = 0;
                   break;
                }
            }

            if(v4_2 == 0) {
                return;
            }

            AdsAppItem.a(arg27, this.alertText, this.appName, "", new com.ss.android.article.base.feature.model.c(this, arg27, v6, this.id, ((long)arg28), v12, arg29, arg30, arg31));
        }
    }

    final void a(Context arg25, DownloadShortInfo arg26, DownloadInfoChangeListener arg27, long arg28, long arg30, JSONObject arg32, int arg33) {
        JSONObject v2_2;
        String v4 = null;
        String v16 = null;
        switch(arg33) {
            case 1: {
                v4 = "feed_download_ad";
                v16 = "embeded_ad";
                break;
            }
            case 2: {
                v4 = "detail_download_ad";
                v16 = "detail_ad";
                break;
            }
            case 3: {
                v4 = "comment_download_ad";
                v16 = "comment_ad";
                break;
            }
            case 4: {
                v4 = "wap";
                v16 = "wap";
                break;
            }
            case 5: {
                v4 = "detail_download_ad";
                v16 = "detail_ad";
                break;
            }
            case 6: {
                v16 = "detail_download_ad";
                v4 = "detail_download_ad";
                break;
            }
            case 7: {
                v16 = "feed_download_ad";
                v4 = "feed_download_ad";
                break;
            }
        }

        this.b(arg33);
        if(arg30 == 2 && arg26 != null && arg26.b == 16) {
            if(arg33 == 7) {
                this.a(arg25, v4, "click", this.id, 2, arg32, arg33);
                this.a(arg25, v4, "click_start", this.id, 2, arg32, arg33);
            }
            else if(arg33 == 6) {
                this.a(arg25, v4, "click_start", this.id, 2, arg32, arg33);
            }
        }

        if(arg30 == 2 && !this.a) {
            this.a(arg25, v16, "click", this.id, 2, arg32, arg33);
            this.a = true;
        }

        if(AppData.getInstance().allowInsideDownloadManager()) {
            if(arg26 != null && !AppUtil.a(arg25, this.version, this.pkg)) {
                DownloadAdapterProxy.inst().handleStatusClick(arg25, arg26.b, arg26.id, null);
                if(arg26.id >= 0) {
                    if(arg26.d == 0) {
                        arg26.b = 16;
                    }

                    switch(arg26.b) {
                        case 1: 
                        case 2: {
                            com.ss.android.article.base.feature.b.a v2 = android.support.design.a.c(this.id);
                            Logger.d("AppAd, handleButtonClick, mId = " + this.id + ", DownloadManager.STATUS_PENDING or DownloadManager.STATUS_RUNNING");
                            if(v2 == null) {
                            }else {
                                v2.c += System.currentTimeMillis() - v2.b;
                                v2.b = System.currentTimeMillis();
                                Logger.d("AppAd, handleButtonClick, mId = " + this.id + ", DownloadManager.STATUS_PENDING or DownloadManager.STATUS_RUNNING, info.spendTime" + v2.c);
                                android.support.design.a.a(v2, false);
                            }
                            break;
                        }
                        case 4: {
                            Logger.d("AppAd, handleButtonClick, mId = " + this.id + ", DownloadManager.STATUS_PAUSED");
                            com.ss.android.article.base.feature.b.a v2 = android.support.design.a.c(this.id);
                            if(v2 == null) {
                            }else {
                                v2.b = System.currentTimeMillis();
                                android.support.design.a.a(v2, false);
                            }
                            break;
                        }
                        case 16: {
                            Logger.d("AppAd, handleButtonClick, mId = " + this.id + ", DownloadManager.STATUS_FAILED");
                            android.support.design.a.a(new com.ss.android.article.base.feature.b.a(this.id, System.currentTimeMillis()), false);
                            MobAdClickCombiner.onAdEvent(arg25, v4, "download_failed", this.id, 0, arg32, this.p);
                          break;
                        }
                    }
                }
                if(arg30 == 2) {
                    AppAd v6 = this;
                    DownloadShortInfo v7 = arg26;
                    Context v8 = arg25;
                    String v9 = v4;
                    long v10 = arg28;
                    long v12 = arg30;
                    JSONObject v14 = arg32;
                    int v15 = arg33;
                    try {
                        v6.a(v7, v8, v9, v10, v12, v14, v15);
                    }
                    catch(Exception v2_1) {
                    }
                }

                if(arg26.id < 0) {
                    return;
                }

                if(arg27 == null) {
                    return;
                }

                DownloadAdapterProxy.inst().registerDownloadListener(arg25, Long.valueOf(arg26.id), arg27, String.valueOf(this.id), arg33, this.logExtra);
                return;
            }

            JSONObject v9_1 = new JSONObject();
            v2_2 = new JSONObject();
            try {
                v2_2.put("url", this.downloadUrl);
                v2_2.put("ad_id", this.id);
                v9_1.put("label", v16);
                v9_1.put("ext_json", v2_2);
            }
            catch(JSONException v2_3) {
            }

            if(Build.VERSION.SDK_INT >= 23) {
                if(!(arg25 instanceof Activity)) {
                    return;
                }

                PermissionsManager.getInstance().requestPermission(
                        (Activity) arg25,
                        new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                        new com.ss.android.article.base.feature.model.d(this, arg25, v9_1, arg27, arg33, v4, arg32, arg30, v16));
                return;
            }

            long v2_4 = AppUtil.a(this.downloadUrl, this.appName, arg25, v9_1);
            if(v2_4 >= 0 && arg27 != null) {
                android.support.design.a.a(new com.ss.android.article.base.feature.b.a(this.id, System.currentTimeMillis()), false);
                DownloadAdapterProxy.inst().registerDownloadListener(arg25, Long.valueOf(v2_4), arg27, String.valueOf(this.id), arg33, this.logExtra);
                MobAdClickCombiner.onAdEvent(arg25, v4, "click_start", this.id, 0, arg32, this.p);
                if(arg30 == 2 && arg33 == 1) {
                    MobAdClickCombiner.onAdEvent(arg25, v16, "feeds_download", this.id, 0, arg32, 2);
                }

                if(android.support.design.a.isTextEmpty(this.alertText)) {
                    return;
                }

                this.o = true;
                return;
            }

            if(v2_4 >= 0) {
                return;
            }

            MobAdClickCombiner.onAdEvent(arg25, v4, "download_failed", this.id, 0, arg32, this.p);
            return;
        }

        JSONObject v3 = new JSONObject();
        v2_2 = new JSONObject();
        try {
            v2_2.put("url", this.downloadUrl);
            v2_2.put("ad_id", this.id);
            v3.put("label", v16);
            v3.put("ext_json", v2_2);
        }
        catch(JSONException v2_3) {
        }

        if(Build.VERSION.SDK_INT >= 23) {
            if(!(arg25 instanceof Activity)) {
                return;
            }

            PermissionsManager.getInstance().requestPermission((Activity) arg25, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, new e(this, arg25, v3));
            return;
        }

        if(AppUtil.a(this.downloadUrl, this.appName, arg25, v3) >= 0) {
            android.support.design.a.a(new com.ss.android.article.base.feature.b.a(this.id, System.currentTimeMillis()), false);
        }
    }

    public void a(Context arg11, String arg12, String arg13, long arg14, long arg16, JSONObject arg18, int arg19) {
        MobAdClickCombiner.onAdEvent(arg11, arg12, arg13, arg14, arg16, arg18, this.p);
    }

    public void a(DownloadShortInfo arg13, Context arg14, String arg15, long arg16, long arg18, JSONObject arg20, int arg21) {
        switch(arg13.b) {
            case 1: 
            case 2: {
                MobAdClickCombiner.onAdEvent(arg14, arg15, "click_pause", arg16, arg18, arg20, this.p);
                break;
            }
            case 4: {
                MobAdClickCombiner.onAdEvent(arg14, arg15, "click_continue", arg16, arg18, arg20, this.p);
                break;
            }
            case 8: {
                if(arg21 == 2) {
                    return;
                }

                if(ToolUtils.isAppInstalled(arg14, this.pkg)) {
                    MobAdClickCombiner.onAdEvent(arg14, arg15, "click_open", arg16, arg18, arg20, this.p);
                    return;
                }

                MobAdClickCombiner.onAdEvent(arg14, arg15, "click_install", arg16, arg18, arg20, this.p);
                break;
            }
        }
    }

    public void initWithJO(JSONObject jo) {
        super.initWithJO(jo);
        if(jo != null) {
            this.appAdSource = jo.optString("source");
            this.appAdSourceLabel = jo.optString("label");
            this.appAdDescription = jo.optString("description");
            this.appAdTitle = jo.optString("title");
            JSONObject imageJO = jo.optJSONObject("image");
            this.imageInfo = ImageInfo.fromJson(imageJO, this.h == 2);
            JSONArray ja = jo.optJSONArray("image_list");
            this.imageList = null;
            ArrayList list = ImageInfo.optImageList(ja, false);
            if(list != null && !((List)list).isEmpty()) {
                this.imageList = list;
            }
            this.webUrl = jo.optString("web_url");
            this.webTitle = jo.optString("web_title");
        }
    }

    private void b(int arg2) {
        this.p = 0;
        switch(arg2) {
            case 3: {
                this.p = 0;
                break;
            }
            case 4: {
                this.p = 5;
                break;
            }
            case 2: 
            case 5: 
            case 6: {
                this.p = 1;
                break;
            }
            case 1: 
            case 7: {
                this.p = 2;
                break;
            }
        }
    }

    public final boolean b() {
        int v3 = 3;
        boolean v0 = false;
        if(this.imageList != null && this.imageList.size() == v3) {
            int v1 = 0;
            while(true) {
                if(v1 >= v3) {
                    break;
                }
                else if(this.imageList.get(v1) != null) {
                    ++v1;
                    continue;
                }

                return v0;
            }

            v0 = true;
        }

        return v0;
    }
}

